package com.dong.javalangchain4jai.listener;

import com.alibaba.fastjson.JSON;
import dev.langchain4j.model.chat.listener.*;

import java.util.Map;

/**
 * 监控模型请求、响应、错误等事件
 *
 * @author xiedongxiao
 */

public class MyChatModelListener implements ChatModelListener {

    @Override
    public void onRequest(ChatModelRequestContext requestContext) {
//        ChatModelListener.super.onRequest(requestContext);

        ChatModelRequest request = requestContext.request();
        Map<Object, Object> attributes = requestContext.attributes();
        System.err.println("MyChatModelListener onRequest-request: " + JSON.toJSONString(request));
        System.err.println("MyChatModelListener onRequest-attributes: " + JSON.toJSONString(attributes));

    }

    @Override
    public void onResponse(ChatModelResponseContext responseContext) {
//        ChatModelListener.super.onResponse(responseContext);

        ChatModelResponse response = responseContext.response();
        ChatModelRequest request = responseContext.request();
        Map<Object, Object> attributes = responseContext.attributes();

        System.err.println("MyChatModelListener onResponse-request: " + JSON.toJSONString(request));
        System.err.println("MyChatModelListener onResponse-attributes: " + JSON.toJSONString(attributes));
        System.err.println("MyChatModelListener onResponse-response: " + JSON.toJSONString(response));
    }

    @Override
    public void onError(ChatModelErrorContext errorContext) {
//        ChatModelListener.super.onError(errorContext);

        Throwable error = errorContext.error();
        ChatModelRequest request = errorContext.request();
        ChatModelResponse partialResponse = errorContext.partialResponse();
        Map<Object, Object> attributes = errorContext.attributes();

        System.err.println("MyChatModelListener onError-request: " + JSON.toJSONString(request));
        System.err.println("MyChatModelListener onError-attributes: " + JSON.toJSONString(attributes));
        System.err.println("MyChatModelListener onError-partialResponse: " + JSON.toJSONString(partialResponse));
        System.err.println("MyChatModelListener onError-error: " + JSON.toJSONString(error));
    }

}
